/*
* Copyright (C) 1997-2001 Id Software, Inc.
* 
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
* 
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE.
* 
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*  
*/

// Created on 27.12.2003 by RST.

// $Id: Info.java,v 1.7 2006/01/10 13:09:18 hzi Exp $
using System;
using Defines = Suake2.UI.Defines;
using Com = Suake2.UI.qcommon.Com;
namespace Suake2.UI.game
{
	
	public class Info
	{
		
		/// <summary> Returns a value for a key from an info string. </summary>
		public static System.String Info_ValueForKey(System.String s, System.String key)
		{
			
			SupportClass.Tokenizer tk = new SupportClass.Tokenizer(s, "\\");
			
			while (tk.HasMoreTokens())
			{
				System.String key1 = tk.NextToken();
				
				if (!tk.HasMoreTokens())
				{
					Com.Printf("MISSING VALUE\n");
					return s;
				}
				System.String value1 = tk.NextToken();
				
				if (key.Equals(key1))
					return value1;
			}
			
			return "";
		}
		
		/// <summary> Sets a value for a key in the user info string.</summary>
		public static System.String Info_SetValueForKey(System.String s, System.String key, System.String value_Renamed)
		{
			
			if (value_Renamed == null || value_Renamed.Length == 0)
				return s;
			
			if (key.IndexOf('\\') != - 1 || value_Renamed.IndexOf('\\') != - 1)
			{
				Com.Printf("Can't use keys or values with a \\\n");
				return s;
			}
			
			if (key.IndexOf(';') != - 1)
			{
				Com.Printf("Can't use keys or values with a semicolon\n");
				return s;
			}
			
			if (key.IndexOf('"') != - 1 || value_Renamed.IndexOf('"') != - 1)
			{
				Com.Printf("Can't use keys or values with a \"\n");
				return s;
			}
			
			if (key.Length > Defines.MAX_INFO_KEY - 1 || value_Renamed.Length > Defines.MAX_INFO_KEY - 1)
			{
				Com.Printf("Keys and values must be < 64 characters.\n");
				return s;
			}
			
			System.Text.StringBuilder sb = new System.Text.StringBuilder(Info_RemoveKey(s, key));
			
			if (sb.Length + 2 + key.Length + value_Renamed.Length > Defines.MAX_INFO_STRING)
			{
				
				Com.Printf("Info string length exceeded\n");
				return s;
			}
			
			sb.Append('\\').Append(key).Append('\\').Append(value_Renamed);
			
			return sb.ToString();
		}
		
		/// <summary> Removes a key and value from an info string. </summary>
		public static System.String Info_RemoveKey(System.String s, System.String key)
		{
			
			System.Text.StringBuilder sb = new System.Text.StringBuilder(512);
			
			if (key.IndexOf('\\') != - 1)
			{
				Com.Printf("Can't use a key with a \\\n");
				return s;
			}
			
			SupportClass.Tokenizer tk = new SupportClass.Tokenizer(s, "\\");
			
			while (tk.HasMoreTokens())
			{
				System.String key1 = tk.NextToken();
				
				if (!tk.HasMoreTokens())
				{
					Com.Printf("MISSING VALUE\n");
					return s;
				}
				System.String value1 = tk.NextToken();
				
				if (!key.Equals(key1))
					sb.Append('\\').Append(key1).Append('\\').Append(value1);
			}
			
			return sb.ToString();
		}
		
		/// <summary> Some characters are illegal in info strings because they can mess up the
		/// server's parsing.
		/// </summary>
		public static bool Info_Validate(System.String s)
		{
			return !((s.IndexOf('"') != - 1) || (s.IndexOf(';') != - 1));
		}
		
		private static System.String fillspaces = "                     ";
		
		public static void  Print(System.String s)
		{
			
			System.Text.StringBuilder sb = new System.Text.StringBuilder(512);
			SupportClass.Tokenizer tk = new SupportClass.Tokenizer(s, "\\");
			
			while (tk.HasMoreTokens())
			{
				
				System.String key1 = tk.NextToken();
				
				if (!tk.HasMoreTokens())
				{
					Com.Printf("MISSING VALUE\n");
					return ;
				}
				
				System.String value1 = tk.NextToken();
				
				sb.Append(key1);
				
				int len = key1.Length;
				
				if (len < 20)
				{
					sb.Append(fillspaces.Substring(len));
				}
				sb.Append('=').Append(value1).Append('\n');
			}
			Com.Printf(sb.ToString());
		}
	}
}